package com.faselbaum.stalkme.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.faselbaum.stalkme.models.ProfileInfo;
import com.faselbaum.stalkme.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ProfileInfoArrayAdapter extends ArrayAdapter<ProfileInfo>
{
	
	private List<ProfileInfo> _profiles = new ArrayList<ProfileInfo>();
	private LayoutInflater _inflater;

	public ProfileInfoArrayAdapter(Context context, int textViewResourceId, List<ProfileInfo> objects)
	{
		super(context, textViewResourceId, objects);
		_inflater = LayoutInflater.from(context);
		_profiles = objects;
		// TODO Auto-generated constructor stub
	}
	
	public int getCount()
	{
		return _profiles.size();
	}
	
	public ProfileInfo getItem(int index)
	{
		return _profiles.get(index);
	}
	
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		
		ViewHolder holder;
		ProfileInfo info = _profiles.get(position);
		
        if (convertView == null) 
        {
            convertView = _inflater.inflate(R.layout.list_item_profile_info, null);

            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.icon = (ImageView) convertView.findViewById(R.id.imageView_profilePicture);
            holder.text = (TextView) convertView.findViewById(R.id.textView_profileName);
            holder.progressbar = (ProgressBar) convertView.findViewById(R.id.progressBar_distance);

            convertView.setTag(holder);
        }
        else 
        {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }
        
        if(info.getImagePath()==null) 
        {
        	//holder.icon.setImageBitmap(BitmapFactory.decodeResource(this.getContext().getResources(),R.drawable.grape));
        }
        else
        {
        	try 
        	{
        		Bitmap bitmap = BitmapFactory.decodeStream(this.getContext().getResources().getAssets().open(info.getImagePath()));
        		holder.icon.setImageBitmap(bitmap);
        	}
        	catch (IOException e)
        	{
        		 e.printStackTrace();
        	}      	
        }
		
        if(info.getFirstName()!=null) holder.text.setText(info.getFirstName());
        
        int signalStrength = (int)(holder.progressbar.getMax() * info.getBluetoothDeviceInfo().getRelativeRSSI());
        //int signalStrength = 100 + info.getBluetoothDeviceInfo().getRSSI();
        holder.progressbar.setProgress(signalStrength);

        return convertView;
	}
	
    static class ViewHolder 
    {
        ImageView icon;
        TextView text;
        ProgressBar progressbar;
    }

}
